//
//  PTOtherSchedule.swift
//  PointTest28
//
//  Created by sc-chen on 2023/6/28.
//

import SwiftUI
import EventKit

struct PTOtherSchedule: View {
    @State private var msgContent = ""
    
    @State private var isShowingHUD = false
    @State private var detailTxt = "不支持开发功能"
    
    @State private var pt_events: [[String : String]] = [
        //["date":"2023-08-10 星期四","eventName":"今日日程"],
    ]
    
    var body: some View {
        ZStack {
            ScrollView {
                VStack {
                    HStack {
                        Text("写入")
                            .padding()
                            .font(.system(size: 15))
                        Spacer()
                        Button {
                            addEventToCalendar()
                        } label: {
                            Text("确定")
                                .frame(width: 80, height: 30)
                                .foregroundColor(Color.white)
                                .background(Color.blue)
                                .cornerRadius(5)
                                .font(.system(size: 14))
                        }
                        .buttonStyle(.borderless)
                    }.padding(.trailing,10)
                        .padding(.bottom,10)
                    
                    TextField("请输入", text: $msgContent)
                        .overlay(
                            RoundedRectangle(cornerRadius: 5)
                                .stroke(Color.gray, lineWidth: 1)
                        )
                        .frame(height: 40)
                    
                    List(Array(pt_events.enumerated()), id: \.element) { idx,dict in
                        HStack {
                            VStack(alignment: .leading) {
                                Text("\(dict["date"] ?? "")")
                                    .padding(EdgeInsets(top: 5, leading: 0, bottom: 5, trailing: 5))
                                Text("\(dict["eventName"] ?? "")")
                                    .padding((EdgeInsets(top: 5, leading: 0, bottom: 5, trailing: 5)))
                            }
                            Spacer()
                            VStack(alignment: .trailing) {
                                Text("删除")
                                    .frame(width: 80, height: 30)
                                    .foregroundColor(Color.red)
                                    .overlay(
                                        RoundedRectangle(cornerRadius: 5)
                                            .stroke(Color.red, lineWidth: 1)
                                    )
                                    .padding()
                                    .onTapGesture {
                                        deleteAction(idx: idx)
                                    }
                            }
                        }
                    }
                    Spacer()
                }
            }
            PTProgressHud(isPresented: $isShowingHUD, text: $detailTxt)
        }
    }
    
    func deleteAction(idx: Int) {
        msgContent = ""
        let item = pt_events[idx];
        if let eventName = item["eventName"] {
            pt_events.remove(at: idx)
            deleteEventByContent(eventContent: eventName)
            showHud(text: "删除成功")
        }
    }
    
    func deleteEventByContent(eventContent: String) {
        showHud(text: "watchOS不支持删除功能")
    }
    
    func addEventToCalendar() {
        showHud(text: "watchOS不支持写入功能")
    }
    
    func date2String(_ date:Date, dateFormat:String = "yyyy-MM-dd") -> String {
        let formatter = DateFormatter()
        formatter.locale = Locale.init(identifier: "zh_CN")
        formatter.dateFormat = dateFormat
        let date = formatter.string(from: date)
        return date
    }
    func weekdayStringFromDate() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.locale = Locale.current
        dateFormatter.dateFormat = "yyyy-MM-dd"
        guard let formatDate = dateFormatter.date(from: date2String(Date())) else { return "" }
        let calendar = Calendar.current
        let weekDay = calendar.component(.weekday, from: formatDate)
        switch weekDay {
        case 1:
            return "星期日"
        case 2:
            return "星期一"
        case 3:
            return "星期二"
        case 4:
            return "星期三"
        case 5:
            return "星期四"
        case 6:
            return "星期五"
        case 7:
            return "星期六"
        default:
            return ""
        }
    }
    
    func getLastDay() -> Date {
        let date = NSDate()
        let lastTime: TimeInterval = -(24*60*60) // 往前减去一天的秒数，昨天
        //let nextTime: TimeInterval = 24*60*60 // 这是后一天的时间，明天
        let lastDate = date.addingTimeInterval(lastTime)
        //let lastDay = dateFormatter.string(from: lastDate!)
        return lastDate as Date
    }
    
    private func showHud(text: String) {
        isShowingHUD = true
        detailTxt = text
        DispatchQueue.main.asyncAfter(deadline: .now()+2){
            isShowingHUD = false
        }
    }
}

struct PTOtherSchedule_Previews: PreviewProvider {
    static var previews: some View {
        PTOtherSchedule()
    }
}
